package cn.kgc.ybis.service.impl;


import cn.kgc.ybis.common.Dto;
import cn.kgc.ybis.common.DtoUtil;
import cn.kgc.ybis.mapper.ManagerMapper;
import cn.kgc.ybis.mapper.SchoolManagerMapper;
import cn.kgc.ybis.mapper.TeacherMapper;
import cn.kgc.ybis.mapper.UserMapper;
import cn.kgc.ybis.pojo.*;
import cn.kgc.ybis.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    TeacherMapper teacherMapper;
    @Resource
    ManagerMapper managerMapper;
    @Resource
    SchoolManagerMapper schoolManagerMapper;
    //學生登录
    @Override
    public Dto login(String uPhone, String uPassword) {
        User user = userMapper.login(uPhone, uPassword);
        if (uPhone == null || uPassword == null) {
            return DtoUtil.returnFail("用户名或者密码不能为空!!", "000");
        }
        if (user == null) {
            return DtoUtil.returnFail("没有此用户", "111");
        }
        if (uPassword.equals(user.getuPassword())) {
            return DtoUtil.returnDataSuccess("登录成功!!");
        } else {
            return DtoUtil.returnFail("登录失败,账号或者密码错误!!!", "333");
        }

    }
    //老师登录
    @Override
    public Dto login2(String tPhone, String tPassword) {
        Teacher teacher = teacherMapper.login2(tPhone, tPassword);
        if (tPhone == null || tPassword == null) {
            return DtoUtil.returnFail("用户名或者密码不能为空!!", "000");
        }
        if (teacher == null) {
            return DtoUtil.returnFail("没有此用户", "111");
        }
        if (tPassword.equals(teacher.gettPassword())) {
            return DtoUtil.returnDataSuccess("登录成功!!");
        } else {
            return DtoUtil.returnFail("登录失败,账号或者密码错误!!!", "333");
        }
    }

    @Override
    public Dto login4(String smPhone, String smPassword) {
        Manager manager = managerMapper.login4(smPhone, smPassword);
        if (smPhone == null || smPassword == null) {
            return DtoUtil.returnFail("用户名或者密码不能为空!!", "000");
        }
        if (manager == null) {
            return DtoUtil.returnFail("没有此用户", "111");
        }
        if (smPassword.equals(manager.getSmPassword())) {
            return DtoUtil.returnDataSuccess("登录成功!!");
        } else {
            return DtoUtil.returnFail("登录失败,账号或者密码错误!!!", "333");
        }
    }

    //查询teacher
    @Override
    public Teacher selectTeacherByEx(String tPhone, String tPassword) {
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andTPhoneEqualTo(tPhone).andTPasswordEqualTo(tPassword);
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        return teachers.get(0);
    }
    //查询smanager
    @Override
    public SchoolManager selectSchoolManager(String smPhone, String smPassword) {
        SchoolManagerExample schoolManagerExample = new SchoolManagerExample();
        schoolManagerExample.createCriteria().andSmPhoneEqualTo(smPhone).andSmPasswordEqualTo(smPassword);
        List<SchoolManager> schoolManagers = schoolManagerMapper.selectByExample(schoolManagerExample);
        return schoolManagers.get(0);
    }
    //查询user
    @Override
    public User selectUserByEx(String uPhone, String uPassword) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUPhoneEqualTo(uPhone);
        List<User> users = userMapper.selectByExample(userExample);
        return users.get(0);
    }
}
